package task2;

import java.util.Random;

public class Producer implements Runnable {

	private int _ID;
	private String myMessage;
	private CyclicBufferWithLock<String> buffer;
	private static int STEPS_QUANTITY = 10;

    public Producer(int ID, CyclicBufferWithLock<String> buffer, final String message) {
    	this._ID = ID;
        this.buffer = buffer;
        myMessage = new String();
        myMessage = myMessage.concat(message);
    }

    public void run() {
        Random random = new Random();        

        for (int i = 0; i < STEPS_QUANTITY; i++) {
        	try {
        		if (i < STEPS_QUANTITY - 1) {
        			buffer.put("From: " + _ID + ": " + myMessage + " STEP: " + i);
        		} else {
        			buffer.put("From: " + _ID + ": DONE");
        		}
			} catch (InterruptedException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
            
            try {
                Thread.sleep(random.nextInt(1000));
            } catch (InterruptedException e) { }
        }        
    }
}